package org.spring.blog.manager;

import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CountDownLatch;

/**
 * 管理CountDownLatch
 * insert delete query update
 */
@Component
public class CountDownLatchManager {
    Map<String, CountDownLatch> latchMap = new HashMap<>();


    public CountDownLatch getLatch(String _loginId) {
        CountDownLatch countDownLatch = latchMap.get(_loginId);

        if (countDownLatch == null) {
            countDownLatch = new CountDownLatch(1);
            latchMap.put(_loginId, countDownLatch);
        }

        return countDownLatch;
    }

    public void deleteLatch(String loginId) {
        latchMap.remove(loginId);
    }

    public void onPhoneDoLogin(String _loginId) {
        CountDownLatch countDownLatch = latchMap.get(_loginId);

        if (countDownLatch != null) {
            countDownLatch.countDown();
        }
    }
}
